#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash allhic-0.9.13.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi
[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget git tar
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
echo 'allhic安装'
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
wget -t 40 -c -P $buildpath https://github.com/tanghaibao/allhic/archive/refs/tags/v0.9.13.tar.gz
tar xf $buildpath/v0.9.13.tar.gz -C $installpath/go-1.18.2/gopath/src
echo 'main.go编译'
cd $installpath/go-1.18.2/gopath/src/allhic-0.9.13
go mod init allhic
go mod tidy
mkdir -p $installpath/allhic-0.9.13/bin
go build -o $installpath/allhic-0.9.13/bin/allhic -v cmd/main.go
cat>"$installpath/allhic-0.9.13/allhic_modulefiles"<<EOF
#%Module1.0
conflict allhic
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set allhic \$pwd
setenv allhic \$allhic
prepend-path PATH \$allhic/bin
EOF
